﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;

namespace Perseus.WCF.Core.Host
{
    public class ConsoleHost<T> : IHost<T>
    {
        public string baseUrl;
        public string serviceName;

        public ConsoleHost(string baseUrl, string serviceName)
        {
            this.baseUrl = baseUrl;
            this.serviceName = serviceName;
        }

        public void Host()
        {
            string tabSpace = "    ";

            SetHostWindow();

            Uri[] baseAddress = new Uri[] { new Uri(baseUrl) };

            using (ServiceHost host = new ServiceHost(typeof(T), baseAddress))
            {
                host.Open();

                Console.WriteLine();
                Console.WriteLine(tabSpace + "===========================================");
                Console.WriteLine(tabSpace + "      Service: {0} is now running ... ", serviceName);
                Console.WriteLine(tabSpace + "      Press any key to stop it ... ");
                Console.WriteLine(tabSpace + "===========================================");
                Console.WriteLine();
                Console.Read();

                host.Close();
            }
        }

        private void SetHostWindow()
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.BackgroundColor = ConsoleColor.Black;
            Console.SetWindowPosition(Console.WindowLeft, Console.WindowTop);
        }
    }
}
